﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JzSport.WebApp.Promotional
{
    public partial class CreateAD : System.Web.UI.Page
    {
        public static object s_lockDirObj = new object();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _ctlEditBeginTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                _ctlEditEndTime.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm");
            }
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            string title = _ctlEditTitle.Text.Trim();
            string city = _ctlEditCity.Text.Trim();

            DateTime beginTime = DateTime.Now;
            if (!DateTime.TryParse(_ctlEditBeginTime.Text, out beginTime))
            {
                Response.Write("<script>alert('创建失败!');</script>");
                return;
            }
            DateTime endTime = DateTime.Now;
            if (!DateTime.TryParse(_ctlEditEndTime.Text, out endTime))
            {
                Response.Write("<script>alert('创建失败!');</script>");
                return;
            }
            if (beginTime >= endTime || endTime <= DateTime.Now)
            {
                Response.Write("<script>alert('创建失败!');</script>");
                return;
            }

            string linkUrl = _ctlEditLink.Text.Trim();
            string previewImageUrl = string.Empty;
            string sponsor = _ctlEditSponsor.Text.Trim();

            if (string.IsNullOrEmpty(title)
                || string.IsNullOrEmpty(linkUrl)
                || string.IsNullOrEmpty(city)
                || string.IsNullOrEmpty(sponsor))
            {
                Response.Write("<script>alert('创建失败!');</script>");
                return;
            }

            #region 上传图片
            if (_ctlFile.HasFile)
            {
                string dir = HttpContext.Current.Server.MapPath("~/Resources/Promotional/Images");
                if (!System.IO.Directory.Exists(dir))
                {
                    lock (s_lockDirObj)
                    {
                        if (!System.IO.Directory.Exists(dir))
                        {
                            System.IO.Directory.CreateDirectory(dir);
                        }
                    }
                }
                string[] segments = _ctlFile.FileName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                Guid imageId = Guid.NewGuid();
                string targetPath = string.Format("{0}/{1}.{2}", dir, imageId, segments.Last());
                _ctlFile.SaveAs(targetPath);
                previewImageUrl = string.Format("./Resources/Promotional/Images/{0}.{1}", imageId, segments.Last());
            }
            else
            {
                Response.Write("<script>alert('创建失败,没有选择图片!');</script>");
                return;
            }
            #endregion

            PromotionalActivityService service = new PromotionalActivityService();
            ServiceResult<bool> result = service.Create(title,city, sponsor, beginTime, endTime, previewImageUrl, linkUrl);
            if (result.ErrorCode == ErrorCode.NoError && result.Data)
            {
                Response.Write(string.Format("<script>alert('创建成功!');window.location.href='{0}';</script>", Request.RawUrl));
            }
            else
            {
                Response.Write("<script>alert('创建失败!');</script>");
            }
        }
    }
}